package com.launch.wlfw.controller;


import cn.hutool.crypto.SecureUtil;
import com.launch.wlfw.common.Common;
import com.launch.wlfw.common.Result;
import com.launch.wlfw.pojo.Admin;
import com.launch.wlfw.pojo.ReaderInfo;
import com.launch.wlfw.service.AdminService;
import com.launch.wlfw.service.ReaderInfoService;
import org.apache.log4j.Logger;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * 登录控制器
 *
 * @author liuwei
 * @date 2023/07/02
 */
@Controller
public class LoginController {

    /**
     * 日志记录器
     */
    private static final Logger logger = Logger.getLogger(LoginController.class);

    /**
     * 管理服务
     */
    @Resource
    private AdminService adminService;

    /**
     * 读者服务
     */
    @Resource
    private ReaderInfoService readerService;

    /**
     * 复述,模板
     */
    @Resource
    private RedisTemplate<String , Object> redisTemplate;

    /**
     * 登录
     * 登录页面的转发
     *
     * @return {@link String}
     */
    @GetMapping("/login")
    public String login(){
        return "login";
    }

    /**
     *
     * 登录验证
     *
     * @param request 请求
     * @param model   模型
     * @return {@link String}
     */
    @RequestMapping("/loginIn")
    public String loginIn(HttpServletRequest request, Model model){
        //获取用户名与密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        Admin admin = adminService.queryUserByNameAndPassword(username,SecureUtil.md5(password));
        ReaderInfo readerInfo = readerService.queryUserInfoByNameAndPassword(username,SecureUtil.md5(password));

        if(admin==null && readerInfo==null){
            model.addAttribute("msg","用户名或密码错误");
            logger.error("当前用户 : " + username + " , 密码 : " + password + " 登录失败");
            return "login";
        }

        redisTemplate.opsForValue().set( username, session.getId() , 60 * 60 * 24 , TimeUnit.SECONDS);
        session.setAttribute("username" , username);
        session.setAttribute(Common.READER_INFO_USER, admin==null ? readerInfo : admin);
        session.setAttribute(Common.READER_INFO_TYPE , admin==null ? "reader" : "admin");
        session.setMaxInactiveInterval(60 * 60 * 24);
        logger.debug("当前用户 : " + username + " , 密码 : " + password + " 登录成功");
        return "index";
    }

    /**
     * 登录了
     * 退出功能
     *
     * @param request 请求
     * @return {@link String}
     */
    @GetMapping("loginOut")
    public String loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();//注销
        return "/login";
    }


    /**
     * 登录变化
     *
     * @param id    id
     * @param model 模型
     * @return {@link String}
     */
    @RequestMapping("/updatePassword")
    public String loginChange(Integer id, Model model) {
        return "pwdUpdate/updatePwd";
    }


    @RequestMapping("/updatePwdSubmit2")
    @ResponseBody
    public Result<Object> updatePwd(String oldPwd , String newPwd , HttpServletRequest request){
        Object object = request.getSession().getAttribute("user");
        if(object instanceof Admin){
            Admin admin = (Admin) object;
            adminService.updateAdminPassword(admin.getId() , oldPwd ,newPwd);
        }else{
            ReaderInfo readerInfo = (ReaderInfo) object;
            readerService.updateReaderPassword(readerInfo.getId() , oldPwd ,newPwd);
        }
        return Result.success();
    }
}